package com.notify;

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.notify.Tools;

public class LoginActivity extends Activity {
	
	private ProgressDialog pDialog;
	TimerTask scanTask;
	final Handler handler = new Handler();
	Timer t = new Timer();
	EditText txtUsername, txtServer, txtPassword;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		final Button button = (Button) findViewById(R.id.login);

		txtUsername = (EditText) findViewById(R.id.email);
		txtPassword = (EditText) findViewById(R.id.password);
		txtServer = (EditText) findViewById(R.id.server);

		Tools tools = new Tools();

		String username = tools.getData(getApplicationContext(), getResources()
				.getString(R.string.username), "");

		String password = tools.getData(getApplicationContext(), getResources()
				.getString(R.string.password), "");

		final String server = tools.getData(getApplicationContext(),
				getResources().getString(R.string.server), "");

		txtUsername.setText(username);
		txtPassword.setText(password);
		txtServer.setText(server);

		button.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {

				new LoginProccedure().execute();

			}
		});

	}
	
	class LoginProccedure extends AsyncTask<String, String, String> {

		String isLoged;
		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(LoginActivity.this);
			pDialog.setMessage("Loading...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0)
		{

			isLoged = new Tools()
					.login(txtUsername.getText().toString(), txtPassword
							.getText().toString(), txtServer.getText()
							.toString(), getApplicationContext());

			isLoged = isLoged.replaceAll("\\s+", "");

					
			return null;
		}
		
		protected void onPostExecute(String file_url) {

			pDialog.dismiss();
			// updating UI from Background Thread

			runOnUiThread(new Runnable() {
				public void run() {
					if (isLoged.equals("ok")) {
						Tools tools = new Tools();
						tools.saveData(getApplicationContext(), getResources()
								.getString(R.string.username), txtUsername
								.getText().toString());
						tools.saveData(getApplicationContext(), getResources()
								.getString(R.string.password), txtPassword
								.getText().toString());
						tools.saveData(getApplicationContext(), getResources()
								.getString(R.string.server), txtServer.getText()
								.toString());

						Intent in = new Intent(getApplicationContext(),
								MainActivity.class);

						in.putExtra("login", "ok");

						finish();

						startActivity(in);

					} else if (isLoged.equals("no")) {
						 Toast.makeText(LoginActivity.this, "Error login ",
						 Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(LoginActivity.this, "Error in server ",
								Toast.LENGTH_LONG).show();
					}
				}
			});

		}
		
	}
}